//
//  GFUserInfoTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFUserInfoTableViewCell.h"

@interface GFUserInfoTableViewCell ()

@property (nonatomic, strong) UIImageView *userImage;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *userTag;
@property (nonatomic, strong) UILabel *likeLabel;
@property (nonatomic, strong) UIView *tapView;
@property (nonatomic, strong) UILabel *tapLabel;
@property (nonatomic, strong) UILabel *countLabel;

@end

@implementation GFUserInfoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.userImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.userImage];
        
        self.userNameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.userNameLabel];
        
        self.userTag = [[UILabel alloc] init];
        self.userTag.layer.cornerRadius = 5;
        self.userTag.layer.masksToBounds = YES;
        self.userTag.font = [UIFont systemFontOfSize:13];
        self.userTag.textAlignment = NSTextAlignmentCenter;
        self.userTag.textColor = [UIColor whiteColor];
        [self.contentView addSubview:self.userTag];
        
        self.likeLabel = [[UILabel alloc] init];
        self.likeLabel.font = [UIFont systemFontOfSize:13];
        self.likeLabel.textColor = [UIColor colorWithRed:0.632 green:0.654 blue:0.659 alpha:1.000];
        [self.contentView addSubview:self.likeLabel];
        
    }
    return self;
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    self.userImage.frame = CGRectMake(10, 10, ScreenWidth / 5, ScreenWidth / 5);
    
    CGRect width = [self.model.name boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.userNameLabel.frame = CGRectMake(10 + ScreenWidth / 5 + 10, 10, width.size.width, ScreenWidth / 20);
    
    CGRect widthTag = [self.model.level_title boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.userTag.frame = CGRectMake(20 + ScreenWidth / 5 + 10 + width.size.width, 10, widthTag.size.width, ScreenWidth / 20);
    
    self.likeLabel.frame = CGRectMake(10 + ScreenWidth / 5 + 10, 10 + ScreenWidth / 20, ScreenWidth - (10 + ScreenWidth / 10 + 10), ScreenWidth / 20);
    
    int x = 0;
    
    if (self.model) {
        
        if (!self.tapView) {
            NSArray *arr = @[self.model.user_tweet_count, self.model.user_follow_count, self.model.user_fans_count];
            NSArray *arrr = @[@"美食足迹", @"关注", @"粉丝"];
            for (int i = 0; i < 3; i++) {
                
                self.tapView = [[UILabel alloc] initWithFrame:CGRectMake(x, 20 + ScreenWidth / 5, ScreenWidth / 3, ScreenWidth / 10)];
                self.tapView.tag = 10000 + i;
                self.tapView.layer.cornerRadius = 5;
                self.tapView.layer.masksToBounds = NO;
                self.tapView.layer.borderWidth = 0.5;
                self.tapView.layer.borderColor = [[UIColor grayColor] CGColor];
                self.tapView.userInteractionEnabled = YES;
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
                tap.numberOfTapsRequired = 1;
                tap.numberOfTouchesRequired = 1;
                [self.tapView addGestureRecognizer:tap];
                [tap addTarget:self action:@selector(tap:)];
                
                [self.contentView addSubview:self.tapView];
                
                self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 3, ScreenWidth / 20)];
                self.countLabel.text = [[arr objectAtIndex:i] stringValue];
                self.countLabel.userInteractionEnabled = NO;
                self.countLabel.textAlignment = NSTextAlignmentCenter;
                self.countLabel.textColor = [UIColor redColor];
                [self.tapView addSubview:self.countLabel];
                
                self.tapLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenWidth / 20, ScreenWidth / 3, ScreenWidth / 20)];
                self.tapLabel.text = [arrr objectAtIndex:i];
                self.tapLabel.userInteractionEnabled = YES;
                self.tapLabel.textAlignment = NSTextAlignmentCenter;
                [self.tapView addSubview:self.tapLabel];
                
                x += ScreenWidth / 3;
                
            }
            
            
        }
        
    }
    
    
}

#pragma mark - 找到vc
- (UIViewController*)viewController {
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

-(void)tap:(UITapGestureRecognizer *)tap {
    
    
    
    if (tap.view.tag == 10000) {
        GFFoodmarkViewController *foodmarkVC = [[GFFoodmarkViewController alloc] init];
        UIViewController *VC = [self viewController];
        foodmarkVC.user_id = self.user_id;
        [VC.navigationController pushViewController:foodmarkVC animated:YES];
    } else if (tap.view.tag == 10001) {
        
        GFLikeFoodViewController *likeFoodVC = [[GFLikeFoodViewController alloc] init];
        UIViewController *VC = [self viewController];
        likeFoodVC.tempArr = self.tempArr;
        likeFoodVC.user_id = self.user_id;
        likeFoodVC.judge = 1;
        [VC.navigationController pushViewController:likeFoodVC animated:YES];
    } else {
        GFLikeFoodViewController *likeFoodVC = [[GFLikeFoodViewController alloc] init];
        UIViewController *VC = [self viewController];
        likeFoodVC.tempArr = self.fansArr;
        likeFoodVC.user_id = self.user_id;
        likeFoodVC.judge = 2;
        [VC.navigationController pushViewController:likeFoodVC animated:YES];
        
    }

}

-(void)setModel:(GFUserInfoModel *)model {
    
    if (_model != model) {
        _model = model;
    }
    
    if (model) {
        
        [self.userImage setImageWithURL:[NSURL URLWithString:model.user_image] placeholderImage:[UIImage imageNamed:@"category_1"]];
        self.userNameLabel.text = model.name;
        if ([model.level_title isEqualToString:@"路人"]) {
            self.userTag.backgroundColor = [UIColor brownColor];
        } else if ([model.level_title isEqualToString:@"食客"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.089 green:0.409 blue:1.000 alpha:1.000];
        } else if ([model.level_title isEqualToString:@"美食爱好者"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.926 green:0.248 blue:0.845 alpha:1.000];
        } else if ([model.level_title isEqualToString:@"美食评论员"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.227 green:0.926 blue:0.249 alpha:1.000];
        } else if ([model.level_title isEqualToString:@"美食专家"]) {
            self.userTag.backgroundColor = [UIColor colorWithRed:0.050 green:0.267 blue:0.078 alpha:1.000];
        }
        self.userTag.text = model.level_title;
        
        self.likeLabel.text = [NSString stringWithFormat:@"分享的美食被%@人喜欢过", model.tobeloved];
        
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
